package com.cyx.exam.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cyx.exam.entity.Class;
import com.cyx.exam.entity.Exam;
import com.cyx.exam.service.ClassService;
import com.cyx.exam.service.ExamService;
import com.cyx.exam.service.QuService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author cyx
 * @since 2021-03-09
 */
@Controller
@RequestMapping("/exam")
public class ExamController {

    @Resource
    private ExamService examService;

    @Resource
    private QuService quService;

    @Resource
    private ClassService classService;

    @GetMapping("/list")
    public String list(Model model,HttpServletRequest request){
        Integer role = (Integer) request.getSession().getAttribute("role");
        //如果是管理员可以查看全部试卷
        if (1==role){
            List<Exam> examList = examService.list();
            model.addAttribute("examList", examList);
        }else{
            //如果是其他用户，获取属于自己班级的试卷列表
            String className = String.valueOf(request.getSession().getAttribute("className"));
            List<Exam> examList = examService.list(new QueryWrapper<Exam>().lambda().eq(Exam::getClassName, className)
                                                    .or().eq(Exam::getClassName,""));
            model.addAttribute("examList", examList);
        }
        return "exam/list";
    }

    @GetMapping("/list2")
    public String view(@RequestParam(required = false, defaultValue = "1", value = "pn") Integer pn,
                        Model model) {
        IPage<Exam> page = examService.selectByPage(pn, 5);
        model.addAttribute("jumpUrl", "list2?pn=");
        //此处得到的page对象,包含了current（当前页）,pages（总页数），total（总记录数），records（记录，就是查询到的List集合）
        model.addAttribute("page", page);
        return "paper/list";
    }
    @GetMapping("/searchsubmit")
    public String searchsubmit(@RequestParam(required = false, defaultValue = "", value = "name")String name,
                               Model model,
                               @RequestParam(required = false, defaultValue = "1", value = "pn") Integer pn) {
        IPage<Exam> page = examService.searchByPage(pn, 5, name);
        model.addAttribute("jumpUrl", "list2?name="+name+"&pn=");
        model.addAttribute("page", page);
        return "paper/list";
    }

    @GetMapping({"/edit/{id}","/edit"})
    public String insert(@PathVariable(value = "id",required = false) Integer id,Model model){
        Exam exam = new Exam();
        if(id != null){
            exam = examService.getById(id);
        }
        List<Class> classList = classService.list();
        model.addAttribute("exam",exam);
        model.addAttribute("classList",classList);
        return "paper/edit";
    }

    @PostMapping("/update")
    public String update(Exam exam,Integer method){
        examService.saveOrUpdate(exam);
        if(method == 1){
            return "redirect:/exam/list2";
        }
        else
            return "redirect:/qu/list/"+exam.getId();
    }

    @GetMapping("/del/{id}")
    @Transactional
    public String delete(@PathVariable(value = "id") Integer id){
        quService.deleteByExamId(id);
        examService.delete(id);
        return "redirect:/exam/list2";
    }

    //计算总分
    @GetMapping("/score/{id}")
    public String Score(@PathVariable("id") Integer id){
        int score = examService.score(id);
        UpdateWrapper<Exam> wrapper = new UpdateWrapper<Exam>();
        wrapper.set("total_score",score);
        wrapper.eq("id",id);
        examService.update(wrapper);
        return "redirect:/exam/list2";
    }
}

